#!/usr/bin/python

import sys
sys.path.append("../viewer/")

import MySQLdb, constants

execfile('../db_setting.rb')

con = MySQLdb.connect(db=DB_NAME, host=DB_HOST, user=DB_USER, passwd=DB_PASS)

cur = con.cursor()
sql = "select x, y, layer, label, id, is_browse_node from nodes order by layer"
cur.execute(sql)
res = cur.fetchall()

for e in res:
    x = float(e[0])
    y = float(e[1])
    layer = int(e[2])
    label = e[3]
    id_str = e[4]
    is_browse_node = e[5]

    for i in range(0, 2):
        map_layer = ((layer - 1) * 2 + i)
        box_x = int(x * constants.MAP_TOP_LAYER_WIDTH  * (2 ** map_layer) / constants.BOUNDING_BOX_WIDTH)
        box_y = int(y * constants.MAP_TOP_LAYER_HEIGHT * (2 ** map_layer) / constants.BOUNDING_BOX_HEIGHT)
        sys.stdout.write(
            '"%s","%s","%s","%d","%d","%d","%f","%f"\n' %
            (id_str, label, is_browse_node, map_layer, box_x, box_y, x, y))
